import { http } from "@/utils/http";
import type { HttpResponse } from "types/request";

import { URL_PREFIX } from "./constants";

export interface TradeAppServiceInfo {
  trade_app_name: string;
  ctp_settings_name: string;
  enabled: boolean;
  service_info: SwarmServiceInfo | null;
}

export interface ContainerInfo {
  hash: string;
  name: string;
  image: string;
  ports: string;
  status: string;
  health: string;
  created: string;
}

export interface SwarmServiceInfo {
  app_name: string;
  created_at: string;
  updated_at: string;
  image: string;
  replicas: string;
  nodes: string[];
}

/**
 * List all trade apps
 */
export function listTradeApps() {
  return http.request<HttpResponse<TradeAppServiceInfo[]>>(
    "get",
    `${URL_PREFIX}/docker/trade_app/list`
  );
}

/**
 * Remove a trade app
 * @param trade_app_name Trade app name
 */
export function removeTradeApp(trade_app_name: string) {
  return http.request<HttpResponse<string>>(
    "post",
    `${URL_PREFIX}/docker/trade_app/remove`,
    {
      params: { trade_app_name }
    }
  );
}

/**
 * Create a trade app
 * @param trade_app_name Trade app name
 */
export function createTradeApp(trade_app_name: string) {
  return http.request<HttpResponse<string>>(
    "post",
    `${URL_PREFIX}/docker/trade_app/create`,
    {
      params: { trade_app_name }
    }
  );
}

/**
 * Upgrade a trade app to latest image
 * @param trade_app_name Trade app name
 */
export function upgradeTradeApp(trade_app_name: string) {
  return http.request<HttpResponse<string>>(
    "post",
    `${URL_PREFIX}/docker/trade_app/upgrade`,
    {
      params: { trade_app_name }
    }
  );
}

/**
 * Upgrade all trade apps to latest image
 */
export function upgradeAllTradeApps() {
  return http.request<HttpResponse<string>>(
    "post",
    `${URL_PREFIX}/docker/trade_app/upgrade/all`
  );
}

/**
 * Restart a trade app
 * @param trade_app_name Trade app name
 */
export function restartTradeApp(trade_app_name: string) {
  return http.request<HttpResponse<string>>(
    "post",
    `${URL_PREFIX}/docker/trade_app/restart`,
    {
      params: { trade_app_name }
    }
  );
}
